package com.dc.bobaoge.project.v2.to.v3.business.core.method.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dc.bobaoge.common.base.entity.message.business.BaseMessageBusinessPo;
import com.dc.bobaoge.common.em.MessageTemplateEnum;
import com.dc.bobaoge.project.base.core.mq.RabbitMqUtil;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessage;
import com.dc.bobaoge.project.base.core.util.MessageCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.function.Consumer;

/**
 * ResolveMessageFunction
 *
 * @author chenxueli
 * @date 2023-12-22 14:25:00
 */
@Slf4j
@Component
public abstract class AbstractResolveMessageMethod<
        T extends BaseMessageBusinessPo,
        MAPPER extends BaseMapper<T>
        > {

    @SuppressWarnings("all")
    @Autowired
    private MAPPER mapper;

    @Resource
    private RabbitMqUtil rabbitMqUtil;
    @Resource
    private MessageCommonUtil messageCommonUtil;

    /**
     * 处理消息信息
     *
     * @param messageId      消息id
     * @param quarter        季度
     * @param oldCoreMessage 旧消息信息
     * @param addSupplier    额外操作
     * @param updateSupplier 额外操作
     * @return 最终的数据
     */
    public final T resolve(Long messageId, String quarter, OldCoreMessage oldCoreMessage, Consumer<T> addSupplier, Consumer<T> updateSupplier) {
        var wrapper = new QueryWrapper<T>().eq("id", messageId).eq("sharding", quarter);
        var item = mapper.selectOne(wrapper);
        if (item == null) {
            item = add(oldCoreMessage, addSupplier);
            // 执行添加媒资的操作
            messageCommonUtil.addImageAsset(messageId, getMessageTemplate().getType(), quarter, oldCoreMessage.getMessageImages());
        } else {
            update(resolvePo(item, oldCoreMessage), oldCoreMessage, updateSupplier);
            messageCommonUtil.updateImageAsset(messageId, getMessageTemplate().getType(), quarter, oldCoreMessage.getMessageImages());
        }
        syncMessage(messageId, getMessageTemplate().getType(), quarter);
        return item;
    }

    /**
     * 新增的方法信息
     *
     * @param oldCoreMessage 旧消息信息
     * @param supplier       额外操作
     * @return 最终的数据
     */
    protected abstract T add(OldCoreMessage oldCoreMessage, Consumer<T> supplier);

    /**
     * 更新的方法信息
     *
     * @param message        新消息信息
     * @param oldCoreMessage 旧消息信息
     * @param supplier       额外操作
     * @return 最终的数据
     */
    protected abstract T update(T message, OldCoreMessage oldCoreMessage, Consumer<T> supplier);

    /**
     * 处理对象信息
     *
     * @param item           新消息信息
     * @param oldCoreMessage 旧消息信息
     */
    protected final T resolvePo(T item, OldCoreMessage oldCoreMessage) {
        item.setId(oldCoreMessage.getMessageId());
        item.setFkUserId(oldCoreMessage.getMessageUserId());

        item.setContent(oldCoreMessage.getMessageContent());
        item.setPublishTime(oldCoreMessage.getMessageDate());
        item.setAddress(oldCoreMessage.getMessageAds());
        item.setAuditRemark(oldCoreMessage.getMessageReason());
        // 消息状态
        setMessageStatus(item, oldCoreMessage);
        return item;
    }

    /**
     * 同步消息信息
     *
     * @param messageId    消息id
     * @param templateType 模板名称
     * @param quarter      季度信息
     */
    private void syncMessage(Long messageId, String templateType, String quarter) {
        rabbitMqUtil.syncMessage(messageId, quarter, templateType);
    }

    /**
     * 设置消息状态 消息状态 0未处理 1已通过 2未通过 3管未审用户删 4管已审用户删 5管未审管理删 6管已审管理删
     *
     * @param item           新消息信息
     * @param oldCoreMessage 旧消息信息
     */
    private void setMessageStatus(T item, OldCoreMessage oldCoreMessage) {
        switch (oldCoreMessage.getMessageState()) {
            case 0:
                item.setAuditStatus(1);
                break;
            case 1:
                item.setAuditStatus(2);
                item.setAuditTime(oldCoreMessage.getMessageVerifyDate());
                break;
            case 2:
                item.setAuditStatus(3);
                item.setAuditTime(oldCoreMessage.getMessageVerifyDate());
                break;
            case 3:
                item.setIsDeleted(1);
                item.setAuditStatus(1);
                break;
            case 5:
                item.setIsDeleted(1);
                item.setAuditStatus(2);
                break;
            case 4:
                item.setAuditStatus(4);
                break;
            case 6:
                item.setAuditStatus(2);
                item.setIsDeleted(1);
                item.setAuditTime(oldCoreMessage.getMessageVerifyDate());
                break;
        }
    }

    /**
     * 获取消息的模板信息
     *
     * @return 消息的模板信息
     */
    public abstract MessageTemplateEnum getMessageTemplate();

}
